package com.shujia.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String URL;
    private static String USERNAME;
    private static String PASSWORD;
    private static String DRIVER;
    public static Connection conn=null;
    static {
        try{
            Properties properties = new Properties();
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("mysql.properties");
            properties.load(is);
            URL=properties.getProperty("url");
            USERNAME=properties.getProperty("username");
            PASSWORD=properties.getProperty("password");
            DRIVER=properties.getProperty("driver");
            Class.forName(DRIVER);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getConn(){
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭所有
    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs)throws Exception{
        rs.close();
        ps.close();
        conn.close();
    }
    //关闭所有
    public static void closeAll(Connection conn, PreparedStatement ps)throws Exception{
        ps.close();
        conn.close();
    }
}
